const sql = require("../sql/mysql");
const users = require("../sql/users");
const jwt = require("jsonwebtoken");

module.exports = (req,res)=>{
    const pw = req.fields;
    const token = req.headers.authorization
    
    jwt.verify(token,"大聪明",(err,data)=>{
        if(err){
            res.send({code:1,msg:"登录失效，请重新登录"})
        }else{
            sql.get(users,{username:data.username,password:pw.oldpw}).then(result=>{
                
                if(result.length>0){   //result为sql.get返回的数组
                    // console.log(result[0].username);
                    sql.set(users,{username:result[0].username},{password:pw.newpw}).then(endres=>{
                        res.send({code:0,msg:"修改成功"})
                    })
                }else{
                    res.send({code:2,msg:"原密码错误"})
                }
            })
        }
    })
}